package com.zlq.add.ticket;


class Window1 implements Runnable {
    private int ticket = 100;
    //Object obj = new Object();
    //Dog dog = new Dog();
    @Override
    public void run() {
//        Object obj = new Object();
        while (true) {
            synchronized (this) {//此时的this:唯一的Window1的对象   //方式二：synchronized (dog) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":卖票，票号为：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}



